package com.genesis.yunnanji.activity;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.genesis.yunnanji.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

public class jPushReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            ActivityManager activityManager =
                    (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            Bundle bundle = intent.getExtras();
            String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
            try {
                JSONObject extraj = new JSONObject(extra);
                String txt = extraj.getString("target_link");
                if (activityManager.getRunningTasks(1).get(0).topActivity.getClassName().contains("com.genesis.yunnanji")) {
                    Intent intent1 = new Intent();
                    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    intent1.setClass(context, WebActivity.class);
                    intent1.putExtra("url", txt);
                    context.startActivity(intent1);


                } else {
                    Intent intentMain = new Intent(context, MainActivity.class);
                    intentMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Intent intent1 = new Intent();
                    Intent[] intents = new Intent[]{intentMain, intent1};

                    intent1.setClass(context, WebActivity.class);
                    intent1.putExtra("url", txt);
                    context.startActivities(intents);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}  